/**
 * 给你一个字符串 s，找到 s 中最长的 回文子串
 * s = "babad"  输出："bab"
 * 
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function (s) {
  let maxStr = ''
  for (let i = 0; i < s.length; i++) {
    const str = getPalindrome(s, i)
    if (str.length > maxStr.length) {
      maxStr = str
    }
  }
  return maxStr

  /**
   * 
   * @param {string} str 
   * @param {number} i 
   */
  function getPalindrome(str, i) {
    let left = i - 1
    let right = i + 1
    while (right < str.length && str.charAt(i) == str.charAt(right)) {
      right++
    }
    while (left >= 0 && right < str.length && str.charAt(left) == str.charAt(right)) {
      left--
      right++
    }
    return str.substring(left + 1, right)
  }
};


console.log(longestPalindrome('babad'))